/**
 * Experimental task capability assertion helpers.
 * WARNING: These APIs are experimental and may change without notice.
 *
 * @experimental
 */

/**
 * Type representing the task requests capability structure.
 * This is derived from ClientTasksCapability.requests and ServerTasksCapability.requests.
 */
interface TaskRequestsCapability {
    tools?: { call?: object };
    sampling?: { createMessage?: object };
    elicitation?: { create?: object };
}

/**
 * Asserts that task creation is supported for tools/call.
 * Used by Client.assertTaskCapability and Server.assertTaskHandlerCapability.
 *
 * @param requests - The task requests capability object
 * @param method - The method being checked
 * @param entityName - 'Server' or 'Client' for error messages
 * @throws Error if the capability is not supported
 *
 * @experimental
 */
export function assertToolsCallTaskCapability(
    requests: TaskRequestsCapability | undefined,
    method: string,
    entityName: 'Server' | 'Client'
): void {
    if (!requests) {
        throw new Error(`${entityName} does not support task creation (required for ${method})`);
    }

    switch (method) {
        case 'tools/call':
            if (!requests.tools?.call) {
                throw new Error(`${entityName} does not support task creation for tools/call (required for ${method})`);
            }
            break;

        default:
            // Method doesn't support tasks, which is fine - no error
            break;
    }
}

/**
 * Asserts that task creation is supported for sampling/createMessage or elicitation/create.
 * Used by Server.assertTaskCapability and Client.assertTaskHandlerCapability.
 *
 * @param requests - The task requests capability object
 * @param method - The method being checked
 * @param entityName - 'Server' or 'Client' for error messages
 * @throws Error if the capability is not supported
 *
 * @experimental
 */
export function assertClientRequestTaskCapability(
    requests: TaskRequestsCapability | undefined,
    method: string,
    entityName: 'Server' | 'Client'
): void {
    if (!requests) {
        throw new Error(`${entityName} does not support task creation (required for ${method})`);
    }

    switch (method) {
        case 'sampling/createMessage':
            if (!requests.sampling?.createMessage) {
                throw new Error(`${entityName} does not support task creation for sampling/createMessage (required for ${method})`);
            }
            break;

        case 'elicitation/create':
            if (!requests.elicitation?.create) {
                throw new Error(`${entityName} does not support task creation for elicitation/create (required for ${method})`);
            }
            break;

        default:
            // Method doesn't support tasks, which is fine - no error
            break;
    }
}
